//Arduino头文件
#include <Arduino.h>

//硬件驱动
#include "net_io.h"

//初始化网络设备IO驱动层
void NET_IO_CLASS::NET_IO_Init(void)
{

    Serial.begin(115200);

}

//发送数据
void NET_IO_CLASS::NET_IO_Send(unsigned char *str, unsigned short len)
{

    Serial.write(str, len);
    Serial.flush();

}

//等待接收完成
bool NET_IO_CLASS::NET_IO_WaitRecive(void)
{

    unsigned char count = 0;
        
    NET_IO_CLASS::dataLen = Serial.available();
    if(NET_IO_CLASS::dataLen == 0)
        return REV_WAIT;
        
    if(NET_IO_CLASS::dataLen == NET_IO_CLASS::dataLenPre)
    {
          for(; count < NET_IO_CLASS::dataLen; count++)
              NET_IO_CLASS::buf[count] = Serial.read();
              
          NET_IO_CLASS::dataLen = 0;
          return REV_OK;
    }

    NET_IO_CLASS::dataLenPre = NET_IO_CLASS::dataLen;

    return REV_WAIT;

}

//清空缓存
void NET_IO_CLASS::NET_IO_ClearRecive(void)
{

    NET_IO_CLASS::dataLen = 0;
    memset(NET_IO_CLASS::buf, 0, sizeof(NET_IO_CLASS::buf));

}
